﻿using System.Linq;
using System.Web;

namespace SecurityModule.Workflows
{
    class WFGetStaticPagesAbout : SimpleWorkflow
    {
        public WFGetStaticPagesAbout()
        {
            Verb = "GET";
            Path = "/StaticPages/About";
        }

        private const string PageTemplate = @"<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>About - The Sales Tax App</title>

    <link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' rel='stylesheet' />

    <link href='https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' rel='stylesheet' />
</head>

<body>
    <div class='container-fluid'>
        <div class='col-sm-12 row'>
            <div class='row col-md-11'>
                <div class='col-sm-2 col-sm-offset-1' style='padding: 15px;'>
                    <a href='{0}/App'>
                        <img src='{0}/Content/images/logo.png' alt='Azavar logo' />
                    </a>
                </div>
            </div>
        </div>
        <div class='col-sm-12'>
            <div class='col-md-8 col-md-offset-2'>
                <div class='panel panel-default'>
                    <div class='panel-body'>
                        <h2>About</h2>
                        <p>
                            You can find all of these libraries in addition to the source code of SecurityModule and technical documentation in Codeplex here:
                            <a href='https://secmod4salestax.codeplex.com' target='_blank'>https://secmod4salestax.codeplex.com</a>
                        </p>
                        <table class='table'>
                            <thead>
                                <tr>
                                    <th></th>
                                    <th>File</th>
                                    <th>Last Update</th>
                                    <th>Hash</th>
                                    <th>Signature</th>
                                </tr>
                            </thead>
                            <tbody>
                                {1}
                            </tbody>
                        </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>";

        private const string RowTemplate = @"<tr class='{0}'>
	<td><i class='fa fa-{1} fa-2x'></i></td>
	<td>{2}</td>
	<td>{3}</td>
	<td>{4}</td>
	<td>{5}</td>
</tr>";

        public override void PreProcessImplementation(HttpContext context)
        {
            // replace the whole response
            context.Response.Filter = new ReplaceFilter(context.Response.Filter,
                context.Response.ContentEncoding,
                content =>
                {
                    var filesInfo = ExpectedAssemblyDetails.Instance.Data.ToDictionary(p => p.Key, p => new AssemblyDetails(context.Server.MapPath("~/bin/") + p.Key));
                    var shortList = (from pair in filesInfo
                                     let icon = pair.Value.PassedValidation.HasValue
                                                    ? pair.Value.PassedValidation.Value
                                                          ? "thumbs-up"
                                                          : "exclamation-triangle"
                                                    : "circle-o"
                                     let bgClass = pair.Value.PassedValidation.HasValue
                                                       ? pair.Value.PassedValidation.Value ? "bg-success" : "bg-danger"
                                                       : "circle-o"
                                     select string.Format(
                                         RowTemplate,
                                         bgClass,
                                         icon,
                                         pair.Key, pair.Value.FileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"),
                                         pair.Value.SHA1Hash,
                                         pair.Value.PublicKeyToken));
                    return string.Format(PageTemplate, "".ToAbsoluteUrl(), string.Join("", shortList));
                }
            );

        }

        public override void PostProcessImplementation(HttpContext context)
        {

        }
    }
}
